package org.jeecg.prod.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Synchronized;
import org.apache.commons.beanutils.BeanUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.prod.entity.MesHaltTime;
import org.jeecg.prod.mapper.MesHaltTimeMapper;
import org.jeecg.prod.service.IMesHaltTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 计划停机配置
 * @Author: LiCY
 * @Date:   2024-01-23
 * @Version: V1.0
 */
@Service
public class MesHaltTimeServiceImpl extends ServiceImpl<MesHaltTimeMapper, MesHaltTime> implements IMesHaltTimeService {

    @Autowired
    private CommonAPI commonAPI;

    @Override
    public void addMesHaltTime(MesHaltTime mesHaltTime) {
        //1获取开始时间和结束时间
        Date startTime = mesHaltTime.getStartTime();
        Date endTime = mesHaltTime.getEndTime();
        Date endTime1 = getDateNextHour(startTime);
        Date endTime2 = getDateNextHour(endTime);
        List<MesHaltTime> mesHaltTimes = new ArrayList<>();
        try{

            while(endTime1.getTime()<=endTime2.getTime()){
                MesHaltTime mesHaltTime1 = cloneObject(mesHaltTime);
                Date date = new Date();
                Date date1 = new Date();
                BeanUtils.copyProperties(date, endTime1);
                BeanUtils.copyProperties(date1, endTime1);
                if (endTime1.getTime()==endTime2.getTime()){
                    mesHaltTime1.setEndTime(mesHaltTime.getEndTime());
                }else{
                    mesHaltTime1.setEndTime(date);
                }
                Long l = ((mesHaltTime1.getEndTime().getTime() - mesHaltTime1.getStartTime().getTime()) / 1000);
                mesHaltTime1.setHours(l);
                mesHaltTimes.add(mesHaltTime1);
                mesHaltTime.setStartTime(date1);
                endTime1 = getDateNextHour(endTime1);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        //2判断结束整点是否大于结束时间
        mesHaltTimes.forEach(this::save);
        //boolean b = this.saveBatch(mesHaltTimes);
    }
    @Synchronized
    private  MesHaltTime cloneObject(MesHaltTime mesHaltTime){
        MesHaltTime mesHaltTime1 = new MesHaltTime();
        Integer recId = commonAPI.getCounterValue("recId.mes_halt_time","0");
        System.out.println("调用主键"+recId);
        mesHaltTime1.setRecId(recId);
        mesHaltTime1.setLine(mesHaltTime.getLine());
        mesHaltTime1.setReason(mesHaltTime.getReason());
        mesHaltTime1.setStartTime(mesHaltTime.getStartTime());
        mesHaltTime1.setEndTime(mesHaltTime.getEndTime());
        mesHaltTime1.setUser(mesHaltTime.getUser());
        mesHaltTime1.setRemark(mesHaltTime.getRemark());
        return mesHaltTime1;
    }
    private Date getDateNextHour(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.HOUR_OF_DAY, 1);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        return instance.getTime();
    }

    private Date getDateCurHour(Date date){
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        return instance.getTime();
    }
}
